/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package views;

import chatsystem.ChatSystemControler;
import chatsystem.User;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.LayoutStyle;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import protocol.TalkID;
import protocol.TextMessage;

/**
 *
 * @author Diane
 */
public class ConversationTab extends JPanel {

    private TalkID idConvers;

    private JScrollPane jScrollPane1;
    private JScrollPane jScrollPane2;
    private JScrollPane jScrollPane3;
    private JSeparator jSeparator1;
    private JTextArea textToReceive;
    private JTextArea textToSend;
    private JButton btnSend;
    private ChatSystemControler csc;

    public ConversationTab(TalkID id, ChatSystemControler csc) {
        idConvers = id;
        this.csc = csc;
        initComponents();
    }

    public TalkID getIdConvers() {
        return idConvers;
    }

    private void initComponents() {
        jScrollPane1 = new JScrollPane();
        jSeparator1 = new JSeparator();
        jScrollPane2 = new JScrollPane();
        textToReceive = new JTextArea();
        jScrollPane3 = new JScrollPane();
        textToSend = new JTextArea();
        btnSend = new JButton();

        jScrollPane2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        textToReceive.setColumns(20);
        textToReceive.setRows(5);
        textToReceive.setEditable(false);
        jScrollPane2.setViewportView(textToReceive);

        jScrollPane3.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane3.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        textToSend.setColumns(20);
        textToSend.setRows(5);
        jScrollPane3.setViewportView(textToSend);

        btnSend.setText("Envoyer");
        btnSend.setEnabled(false);
        btnSend.addActionListener(new ButtonListener());  
        
        textToSend.addKeyListener(new KeyAdapter() {
                @Override
                public void keyTyped(KeyEvent event) {                             
                    if (textToSend.getText().length() > 0) {
                        btnSend.setEnabled(true);
                    } else {
                        btnSend.setEnabled(false);
                    }
                }});
        
        GroupLayout jPanel1Layout = new GroupLayout(this);
        this.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane3, GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSend)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnSend, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane3, GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE))
                .addContainerGap())
        );
    }

    public void displayTextMessage(String s, User fromUser) {
        textToReceive.append("\n"+fromUser.getName()+" says "+s);
    }
    

    class ButtonListener implements ActionListener{

        public void actionPerformed(ActionEvent e) {
            // CLIC sur le bouton Send
            if (e.getSource() == btnSend) {
                try {
                    /* Préparation du paquet à envoyer
                     * Contient l'id de la conversation courante et le texte
                     */
                    // TODO Comment faire si on a plusieurs conversations ouvertes, quelle conversation courante?
                    //TalkID id = csc.getModel().getConversations().get(0).getTid();
                    TextMessage msg = new TextMessage(idConvers, textToSend.getText());
                    csc.sendTextMessage(idConvers, msg);
                    /* On rafraîchit l'affichage */
                    textToSend.setText("");
                    textToReceive.append("\nMe say "+msg.getMsgContent());
                } catch (Exception ex) {
                    System.out.println(ex);
                }
            }
        }
    }
}
